package com.wmyg.antifake.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wmyg.antifake.bean.*;
import com.wmyg.antifake.dao.BrandMapper;
import com.wmyg.antifake.dao.FactoryMapper;
import com.wmyg.antifake.dao.PictureMapper;
import com.wmyg.antifake.service.BrandService;
import com.wmyg.antifake.service.FactoryService;
import com.wmyg.antifake.utils.PicConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("factoryService")
public class FactoryServiceImpl implements FactoryService {

    @Autowired
    private FactoryMapper factoryMapper;

    @Autowired
    private PictureMapper pictureMapper;

    @Override
    public int delete(int id) {
        int result=factoryMapper.deleteFactory(id);
        return result;
    }

    @Override
    public int insert(Factory factory) {
        return factoryMapper.insert(factory);
    }

    @Override
    public int update(Factory factory) {
        return factoryMapper.update(factory);
    }

    @Override
    public Factory selectById(Integer factoryId) {

        Factory factory = factoryMapper.view(factoryId);
        List<Picture> picList = pictureMapper.selectPic(1,String.valueOf(factoryId),null);
        //PictureConvert pictureConvert=PicConvertUtils.getPictureConvertUrl(picList);
        PictureConvert pictureConvert= PicConvertUtils.getPictureConvert(picList);
        factory.setLogoList(pictureConvert.getLogo());
        factory.setImgList(pictureConvert.getPic());
        return factory;
    }

    @Override
    public List<Factory> findAll() {
        return factoryMapper.findAll();
    }

    @Override
    public PageInfo<Factory> selectFindAll(Factory factory) {
        PageHelper.startPage(factory.getPageIndex(), factory.getPageSize(), " factory_id desc");
        List<Factory> billList = factoryMapper.selectFindAll(factory);

        for(Factory factory1 : billList){
            List<Picture> picList = pictureMapper.selectPic(1,String.valueOf(factory1.getFactoryId()),null);
            PictureConvert pictureConvert= PicConvertUtils.getPictureConvert(picList);
            factory1.setFactoryLogo(pictureConvert.getLogo() != null && pictureConvert.getLogo().size() != 0 ? pictureConvert.getLogo().get(0).getPicUrl() : null);
        }
        return new PageInfo(billList);
    }

    @Override
    public int insertOrUpdate(Factory factory) {

       // int result = factoryMapper.insertOrUpdate(factory);
        int result = 0;
        if(factory.getFactoryId() == null)
            result = factoryMapper.insert(factory);
        else
            result = factoryMapper.update(factory);

        List<Picture>  picList = dealPicList(factory,3,factory.getFactoryImg());
        if(picList.size() != 0)
            pictureMapper.insertBatch(picList);

        List<Picture> picList2 = dealPicList(factory,1,factory.getFactoryLogo());
        if(picList2.size() != 0)
            pictureMapper.insertBatch(picList2);


        return result;
    }

    List<Picture> dealPicList(Factory factory, int picType, String img){
        List<Picture> picList=new ArrayList();
        if(StringUtils.isNotEmpty(img)){
            String[] imgArr=img.split(",");
            for(int i=0;i<imgArr.length;i++){
                if(StringUtils.isEmpty(imgArr[i])){
                    continue;
                }
                String [] newArr=imgArr[i].split("&");
                Picture picture=new Picture(null,1,String.valueOf(factory.getFactoryId()),picType,newArr[0], NumberUtils.toInt(newArr[1]),NumberUtils.toInt(newArr[2]));
                picList.add(picture);
            }
        }
        return picList;
    }

}
